package com.aliyun.cloudcallcenter.crm.aliyun.controller;

import java.security.Principal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.aliyun.cloudcallcenter.crm.aliyun.manager.UserManager;
import com.aliyun.cloudcallcenter.crm.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * crm controller
 */
@Controller
public class CrmController {

    @Autowired
    UserManager userManager;

    @RequestMapping(value = "/admin/info/register", method = RequestMethod.POST)
    public Object registerAdminInfo(ModelMap model, Principal principal, HttpServletRequest httpServletRequest) {
        String ak = httpServletRequest.getParameter("ak");
        String sk = httpServletRequest.getParameter("sk");
        // 保存 admin 的 AK, SK
        User user = userManager.updateAkSkByUserName(principal.getName(), ak, sk);
        model.addAttribute("userName", user.getUserName());
        model.addAttribute("ak", user.getAk());
        return new ModelAndView("/home", model);
    }

    @RequestMapping("/users")
    public String listUsers(Model model) {
        List<User> users = userManager.findAll();
        model.addAttribute("userList", users);
        return "users";
    }
}
